package cn.szzsi._webpc;

import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.Specification;
import cn.szzsi.common.model.SpecificationValue;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

import java.util.List;

public class SpecificationController extends Controller{

    public void getSku(){
        String specificationids=getPara("specificationids");
        String specificationArr[]=specificationids.split(",");
        JSONArray jsonArray=new JSONArray();
        for(int i=0;i<specificationArr.length;i++){
            JSONObject jsonSpecification=new JSONObject();//规格
            String specificationid=specificationArr[i];
            Specification specification=Specification.me.findById(specificationid);
            //已删除规格不显示
            if (specification != null && specification.getStatus() == 0) {
                String specificationName=specification.getName();
                jsonSpecification.put("specificationId",specificationid);
                jsonSpecification.put("specificationName",specificationName);
                List<SpecificationValue>specificationValues=SpecificationValue.dao.getBySpecificationId(specificationid);
                JSONArray specificationValueArr=new JSONArray();
                for(SpecificationValue specificationValue:specificationValues){
                    //已删除规格不显示
                    if (specificationValue.getStatus() == 0) {
                        JSONObject jsonSpecificationValue=new JSONObject();//规格值
                        jsonSpecificationValue.put("specificationValueId",specificationValue.getId());
                        jsonSpecificationValue.put("specificationValueName",specificationValue.getName());
                        specificationValueArr.add(jsonSpecificationValue);
                    }
                }
                jsonSpecification.put("specificationValue",specificationValueArr);
                jsonArray.add(jsonSpecification);
            }
        }
        renderJson(Ret.ok("info",jsonArray));

    }

    /**
     * 查询sku价格
     */
    public void getSkuPrice(){
        String specificationValueIds = getPara("specificationValueIds");
        String specificationtype = getPara("specificationtype");
        Product product = Product.getProductBySpecificationvalues(specificationValueIds, specificationtype);
        if (product == null) {
            renderJson(Ret.ok("num",0).set("price", 0).set("marketPrice",0));
            return;
        }else {
            renderJson(Ret.ok("num",(product.getStore()-product.getFreezeStore())).set("price", product.getPrice()).set("marketPrice",product.getMarketPrice()));
        }
    }


}
